<?php

/*
 * Niken Larasati
 * larasati.n@students.amikom.ac.id
 */

class pindah_pergi extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('tanggal');
    }

    function form_pindahpergi() {
        $this->load->model('umum_m');
        $data['kat_pindah'] = $this->umum_m->ambil_data('kat_pindah');
        $data['propinsi'] = $this->umum_m->ambil_data('propinsi');
        $data['jenis_pindah'] = $this->umum_m->ambil_data('jenis_pindah');
        
        $this->template->load('halaman_v', 'pindah_pergi/tambah_pindahpergi_v', $data);
    }

    function tambah() {
        if ($this->input->post('tambah') != NULL) {
            $data['nomor'] = $this->input->post('nomor');
            $data['idpindah_pergi'] = $this->input->post('idpindah_pergi');
            $data['idkat_pindah'] = $this->input->post('idkat_pindah');
            $data['nik'] = $this->input->post('nik');
            $data['no_kk'] = $this->input->post('no_kk');
            $data['alasan'] = $this->input->post('alasan');
            $data['alamat_tujuan'] = $this->input->post('alamat_tujuan');
            $data['rt_tujuan'] = $this->input->post('rt_tujuan');
            $data['rw_tujuan'] = $this->input->post('rw_tujuan');
            $data['dusun_tujuan'] = $this->input->post('dusun_tujuan');
            $data['desa_tujuan'] = $this->input->post('desa_tujuan');
            $data['kecamatan_tujuan'] = $this->input->post('kecamatan_tujuan');
            $data['kabupaten_tujuan'] = $this->input->post('kabupaten_tujuan');
            $data['idpropinsi'] = $this->input->post('idpropinsi');
            $data['kodepos_tujuan'] = $this->input->post('kodepos_tujuan');
            $data['telp_tujuan'] = $this->input->post('telp_tujuan');
            $data['idjenis_pindah'] = $this->input->post('idjenis_pindah');
            $data['statuskk_tinggal'] = $this->input->post('statuskk_tinggal');
            $data['statuskk_pindah'] = $this->input->post('statuskk_pindah');
            $data['tanggal_pergi'] = $this->input->post('tanggal_pergi');
            $data['jumlah_keluargapergi'] = $this->input->post('jumlah_keluargapergi');

            $this->load->model('umum_m');
            if ($this->umum_m->cek_relasi_tabel('penduduk','nik',$data['nik'])>0){
                $this->umum_m->ubah('penduduk',array('status'=>0),'nik',$data['nik']);
                $this->umum_m->tambah('pindah_pergi', $data);
            }
            $this->tampil();
        }
    }

    function ubah($id = NULL) {
        if ($id != NULL) {
            $this->load->model('umum_m');
            $data['kat_pindah'] = $this->umum_m->ambil_data('kat_pindah');
            $data['propinsi'] = $this->umum_m->ambil_data('propinsi');
            $data['jenis_pindah'] = $this->umum_m->ambil_data('jenis_pindah');
            $data['data_pindahpergi'] = $this->umum_m->ambil_data('pindah_pergi', 'nomor = ' . $id)->row();
            $this->template->load('halaman_v', 'pindah_pergi/ubah_pindahpergi_v', $data);
        } else {
            redirect('pindah_pergi');
        }
    }

    function simpan_ubah() {
        if ($this->input->post() != NULL) {
            $pergi['nomor'] = $this->input->post('nomor');
            $pergi['idpindah_pergi'] = $this->input->post('idpindah_pergi');
            $pergi['idkat_pindah'] = $this->input->post('idkat_pindah');
            $pergi['nik'] = $this->input->post('nik');
            $pergi['no_kk'] = $this->input->post('no_kk');
            $pergi['alasan'] = $this->input->post('alasan');
            $pergi['alamat_tujuan'] = $this->input->post('alamat_tujuan');
            $pergi['rt_tujuan'] = $this->input->post('rt_tujuan');
            $pergi['rw_tujuan'] = $this->input->post('rw_tujuan');
            $pergi['dusun_tujuan'] = $this->input->post('dusun_tujuan');
            $pergi['desa_tujuan'] = $this->input->post('desa_tujuan');
            $pergi['kecamatan_tujuan'] = $this->input->post('kecamatan_tujuan');
            $pergi['kabupaten_tujuan'] = $this->input->post('kabupaten_tujuan');
            $pergi['idpropinsi'] = $this->input->post('idpropinsi');
            $pergi['kodepos_tujuan'] = $this->input->post('kodepos_tujuan');
            $pergi['telp_tujuan'] = $this->input->post('telp_tujuan');
            $pergi['idjenis_pindah'] = $this->input->post('idjenis_pindah');
            $pergi['statuskk_pindah'] = $this->input->post('statuskk_pindah');
            $pergi['tanggal_pergi'] = $this->input->post('tanggal_pergi');
            $pergi['jumlah_keluargapergi'] = $this->input->post('jumlah_keluargapergi');

            $this->load->model('umum_m');
            if ($this->umum_m->ubah('pindah_pergi', $pergi, 'nomor', $pergi['nomor'])) {
                redirect('pindah_pergi/tampil');
            }
        }
    }

    function tampil() {
        $this->load->model('pindah_pergi_m');
        $data['pindah_pergi'] = $this->pindah_pergi_m->ambil_data();
        $this->template->load('halaman_v', 'pindah_pergi/tampil_pindahpergi_v', $data);
    }

    function hapus($id = NULL) {
        $this->load->model('umum_m');
        $this->umum_m->hapus_data_tabel('pindah_pergi', array('nomor' => $id));
        $this->tampil();
    }
    
    function cari_nik() {
        $this->load->model('umum_m');
        $data['pindah_pergi'] = $this->umum_m->ambil_data('pindah_pergi');
        $data['penduduk'] = $this->umum_m->ambil_data('penduduk');
        if ($this->input->post() != NULL) {
            $this->load->model('pindah_pergi_m');
            $this->load->model('penduduk_m');
            $data['pindah_pergi'] = $this->pindah_pergi_m->detail_cari(array('pindah_pergi.nik' =>  $this->input->post('nik')));
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.nik' =>  $this->input->post('nik')));
            $data['nik_pindah_pergi'] = $this->umum_m->native_query('select nik from penduduk where nik=' . $this->input->post('nik'))->row();
            $this->template->load('halaman_v', 'pindah_pergi/cari_nikpindahpergi_v', $data);
        } else {
            $this->load->model('penduduk_m');
            $this->load->model('pindah_pergi_m');
            $data['pindah_pergi'] = $this->pindah_pergi_m->detail_cari();
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'pindah_pergi/cari_nikpindahpergi_v', $data);
        }
    }
    
    function cari_nokk() {
        $this->load->model('umum_m');
        $data['pindah_pergi'] = $this->umum_m->ambil_data('pindah_pergi');
        if ($this->input->post() != NULL) {
            $this->load->model('pindah_pergi_m');
            $data['pindah_pergi'] = $this->pindah_pergi_m->detail_cari(array('pindah_pergi.no_kk' =>  $this->input->post('no_kk')));
            $data['kk_pindah_pergi'] = $this->umum_m->native_query('select no_kk from pindah_pergi where no_kk=' . $this->input->post('no_kk'))->row();
            $this->template->load('halaman_v', 'pindah_pergi/cari_kkpindahpergi_v', $data);
        } else {
            $this->load->model('pindah_pergi_m');
            $data['pindah_pergi'] = $this->pindah_pergi_m->detail_cari();
            $this->template->load('halaman_v', 'pindah_pergi/cari_kkpindahpergi_v', $data);
        }
    }

}

?>
